package com.bluetooth.phone;

import android.os.Build;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.bluetooth.phone.multi.BaseViewHolder;
import com.bluetooth.phone.multi.ItemTypeFactoryImp;

import java.util.Collections;
import java.util.List;

/**
 * Created by WJY.
 * Date: 2021-03-16
 * Time: 10:24
 * Description: 拖拽的recyclerView 的  adapter
 */
public class DialogAdapter extends BaseAdapter {
    private static final String TAG = "DialogAdapter";
    private boolean isShowDelete = false;//是否显示删除图标

    public DialogAdapter(List<DockItem> dockItems) {
        super(dockItems);
        mTypeFactory = new ItemTypeFactoryImp();
        setDialogSwapListener(this);
        mDragListener = new DragListener();
    }
    @Override
    public void onBindViewHolder(@NonNull final BaseViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        //删除
        holder.itemView.setOnDragListener(mDragListener);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemDelete(holder.getBindingAdapterPosition(), mSubSwapListener);
                notifyDataSetChanged();//增删时需要全量更新
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public boolean onLongClick(View view) {
                mDragViewHolder = holder;
                // 拖拽后
                startDrag(holder, DialogAdapter.this);
                return true;
            }
        });
    }


    @Override
    protected void onDragItemEnter(int position) {
        onItemDelete(position, mSubSwapListener);
        notifyDataSetChanged();//增删时需要全量更新
    }

    @Override
    public void onItemInsert(DockItem dockItem) {
        Log.e(TAG, " onItemAdd");
        int position = 0;
        for (int i = 0; i < contentList.size(); i++) {
            if (dockItem.getBindingPotion() < contentList.get(i).getBindingPotion()) {
                position = i;
                break;
            }

            if (i + 1 == contentList.size()) {
                contentList.add(contentList.size(), dockItem);
                notifyItemInserted(contentList.size());
                notifyItemRangeChanged(contentList.size(), 0);
                return;
            }
            if (dockItem.getBindingPotion() >= contentList.get(i).getBindingPotion() && dockItem.getBindingPotion() < contentList.get(i + 1).getBindingPotion()) {
                position = i + 1;
                break;
            }

        }
        contentList.add(position, dockItem);
        notifyItemInserted(position);
        notifyItemRangeChanged(position, contentList.size() - position);
    }

    @Override
    public void onItemEndInsert(DockItem dockItem) {

    }

    @Override
    public void onItemMoved(int fromPosition, int toPosition) {

    }

    @Override
    public void onDragItemToMove(int toPosition) {
        mDragViewHolder.itemView.setAlpha(0);
        onItemMoved(mDragViewHolder,toPosition);
    }
   public void onItemMoved(RecyclerView.ViewHolder source, int toPosition ) {
        int fromPosition = source.getBindingAdapterPosition();
        Log.e(TAG, "onItemMove fromPosition" + fromPosition);
        Log.e(TAG, "onItemMove toPosition" + toPosition);
        if (fromPosition < contentList.size() && toPosition < contentList.size()) {
            //交换数据位置
            Collections.swap(contentList, fromPosition, toPosition);
            //刷新位置交换
            notifyItemMoved(fromPosition, toPosition);
        }
    }

}